<?php
$cat1 = $blog->GetCatList($blogid, 'ee_blog_categories1');
$cat = $blog->GetCatList($blogid, 'ee_blog_categories2');
$status = array("1"=>"公开","2"=>"会员查看","3"=>"不公开");
$url = "index.php?Action=Post";
if($_GET['PostId'])
{
	$info = $blog->getInfo($_GET['PostId'],'ee_blog_posts');
	if($info['F_ID_USER_INFO']!= $_SESSION['User_F_ID'])
	{
		echo "您无权编辑此文章";
		exit();
	}
	$url = "index.php?Action=Post&PostId={$_GET['PostId']}";
}
if($_SERVER['REQUEST_METHOD']=='POST')
{
	$data = array();
	$time = time();

	$data['F_ID_CAT1'] = $_POST['cat1id'];
	$data['F_ID_CATEGORIES'] = $_POST['catid'];
	$data['F_ID_USER_INFO'] = $_SESSION['User_F_ID'];
	$data['F_POSTS_MOD_DATE'] = $time;
	$data['F_POSTS_STATUS'] = $_POST['status'];
	$data['F_POSTS_CONTENTS'] = $_POST['chaptercontent'];
	$data['F_POSTS_TITLE'] = $_POST['title'];
	$data['F_POSTS_IS_COMMENTED'] = ($_POST['comments'])?1:0;
	//$data['F_POSTS_UPLOAD_ID'] = $upload; 不应该存在的字段
	if($_GET['PostId'])
	{
		$cate2 = $blog->getInfo($info['F_ID_CATEGORIES'], 'ee_blog_categories2');
		if($blog->UpdateCatPosts($cate2['F_ID'], '-')){
			if($blog->updateData("ee_blog_posts",$_GET['PostId'],$data))
			{
				$blog->UpdateCatPosts($_POST['catid']);
				echo "<script type=\"text/javascript\">";
		   		echo "window.location='index.php?Action=PostList';";
		  		echo "</script>";
				exit();
			}
		}
	}else{
		$data['F_POSTS_ISSUE_DATE'] = $time;
		$data['F_POSTS_VIEWS'] = 0;
		$data['F_POSTS_COMMENTS'] = 0;
		if($blog->insertData('ee_blog_posts',$data)) //判断是否返回了添加文章的唯一id
		{
			$blog->UpdateCatPosts($_POST['catid']);
			echo "<script type=\"text/javascript\">";
	   		echo "window.location='index.php?Action=PostList';";
	  		echo "</script>";
			exit();
		}
	}
}

if($_SERVER['REQUEST_METHOD']=='POST'){
	$upload_data = upload($_FILES['upfile'], UPLOAD_PATH, MAX_UPLOAD_SIZE);
	if(sizeof($upload_data)==3){
		$upload_now_id = $blog->insertData('em_blog_upload', $upload_data);
		$upload_info = $blog->getInfo($upload_now_id, 'em_blog_upload');
		//echo $upload_info['F_UPLOAD_NAME'];
	}
}
?>
<div class="tab-content show" id="chapter-manage">
<a class="current" href="index.php?Action=Post">写文章</a>
<a href="index.php?Action=PostList">文章列表</a>
</div>
</div>
<div id="content">
<div class="admin-form">
<h3>写文章</h3>

<form method="post" name="form2" action="" enctype="multipart/form-data">
<fieldset>
<dl>
  <dt>上传文件：</dt>
  <dd><input type="file" id="upfile" name="upfile"> <input type="submit" value="上传"></dd>
</dl>
</fieldset>
</form>

<form name="form1" method="post" action="<?php echo $url?>">
<fieldset>
  <dl>
    <dt>标题：</dt>
    <dd><input type="text" style="width:600px" id="title" name="title" value="<?php echo $info['F_POSTS_TITLE']?>"></dd>
  </dl>
  <dl>
    <dt>分类：</dt>
    <dd><font>一级分类</font><select name="cat1id">
	<?php
	foreach($cat1 as $val1)
	{
		if(isset($info['F_ID_CAT1']))//判断是否提取了文章分类ID
		{
			if($info['F_ID_CAT1'] == $val1['F_ID']){//判断被编辑文章的分类ID是否与该分类相等
				$selected = "selected";
			}else{
				$selected = "";
			}
		}
		echo "<option value={$val1['F_ID']} {$selected}>".$val1['F_CATEGORIES_NAME']."</option>";
	}
	?>
	</select>
	<font>二级分类</font><select name="catid">
	<?php
	foreach($cat as $value)	//循环输出分类
	{
		if(isset($info['F_ID_CATEGORIES']))//判断是否提取了文章分类ID
		{
			if($info['F_ID_CATEGORIES'] == $value['F_ID']){//判断被编辑文章的分类ID是否与该分类相等
				$selected = "selected";
			}else{
				$selected = "";
			}
		}
	  	echo "<option value={$value['F_ID']} {$selected}>".$value['F_CATEGORIES_NAME']."</option>";

	}
	?>
	</select></dd>
  </dl>
  <dl>
    <dt>正文：</dt>
    <dd><textarea id="chaptercontent" name="chaptercontent" rows="15" style="width:950px;"><?php
    if(sizeof($upload_data)==3){
     echo "<img src=\"http://localhost/letgo/upload/blog/".$upload_info['F_UPLOAD_NAME']."\">";
    }
	 echo $info['F_POSTS_CONTENTS'];
	?></textarea></dd>
  </dl>
  <dl>
	<dt>是否允许评论：</dt><?php
	$is_commented = $info['F_POSTS_IS_COMMENTED'];
	if($is_commented && ($is_commented==0))				//判断被编辑文章的评论状态是否为0
		$checked = "";
	else
		$checked = "checked";
	//echo $checked;
	?><dd><input type="checkbox" id="comments" name="comments" value="1" <?php echo $checked; ?>></dd>
  </dl>
  <dl>
    <dt>文章状态：</dt>
    <dd><?php
	for($i=1;$i<=3;$i++)									//循环输出文章状态选项
	{
	echo "<label for=\"status$i\">";
	echo "<input name=\"status\" type=\"radio\" id=\"status$i\" value=\"$i\"";
	if(isset($info['F_POSTS_STATUS']))						//判断是否为编辑状态
	{
		if($info['F_POSTS_STATUS'] == $i)					//判断被编辑文章的状态是否和此状态相同
			echo " checked";
	}else{
		if($i == 1)										//默认选定公开状态
			echo " checked";
	}
	echo ">".$status[$i];
	echo "</label> ";
	}
	?></dd>
  </dl>
  <dl>
    <dd class="submit-button"><input type="submit" class="submit" id="post" name="post" value="发 布">
    <input type="reset" class="reset" value="重 写" name="reset"></dd>
  </dl>
</fieldset>
</form>
</div></div>